/**
 * Created by fujia on 2016/5/23.
 */


// 引入 gulp
var gulp = require('gulp');

// 引入组件
var minifycss = require('gulp-minify-css'),//css压缩
    uglify = require('gulp-uglify'),//js压缩
    concat = require('gulp-concat'),//文件合并
    rename = require('gulp-rename'),//文件更名
    notify = require('gulp-notify'),//提示信息
    browserify = require('browserify'),
    source = require('vinyl-source-stream'),
    buffer = require('vinyl-buffer'),
    sass = require('gulp-sass'),
    browserSync = require('browser-sync').create(),
    reload  = browserSync.reload,
    autoprefixer = require('gulp-autoprefixer'),
    combiner = require('stream-combiner2'),
    plumber = require('gulp-plumber');


// 处理sass
gulp.task('autoSass', function () {
    return gulp.src(['sass/*.scss'])
        .pipe(sass().on('error', sass.logError))
        .pipe(gulp.dest('sass/allcss'))
        .pipe(concat('all.css'))
        .pipe(autoprefixer({
            browsers:['last 2 versions','ie 6-8','> 5%','Android >= 4.0'],
            cascade: false
        }))
        .pipe(gulp.dest('dist'));
});


// 出来js
gulp.task('browserify',function(){
    var b = browserify({
        entries: "js/entry.js",
        debug: false
    });
    return b.bundle()
        .pipe(source('all.js'))
        .pipe(buffer())
        .pipe(plumber())
        .pipe(gulp.dest("dist"));
});

// 监视
gulp.task('watch',function(){
    var combined = combiner.obj([
        gulp.watch(['js/*.js'],['browserify']),
        gulp.watch(['sass/*.scss'],['autoSass']),

    ]);

    combined.on('error',console.error.bind(console));
    return combined;
});


gulp.task('default',['watch','autoSass','browserify']);
gulp.task('reload',['watch','autoSass','browserify']);